package com.local.order.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ配置，定义交换机、队列，并绑定
 */

@Configuration
public class RabbitMQConfig {

    @Value("${rabbitmq.order-exchange}")
    private String orderExchange;

    @Value("${rabbitmq.order-unpay-queue}")
    private String orderUnpayQueue;

    @Value("${rabbitmq.order-unpay-routingKey}")
    private String orderUnpayRoutingKey;

    @Value("${rabbitmq.order-unpay-dead-letter-exchange}")
    private String orderUnpayDeadLetterExchange;

    @Value("${rabbitmq.order-unpay-dead-letter-queue}")
    private String orderUnpayDeadLetterQueue;

    @Value("${rabbitmq.order-unpay-dead-letter-routingKey}")
    private String orderUnpayDeadLetterRoutingKey;


    /**
     * 定义dead letter死信交换机
     * @return
     */
    @Bean
    public TopicExchange orderUnpayDeadLetterExchange() {
        return new TopicExchange(this.orderUnpayDeadLetterExchange);
    }

    /**
     * 定义dead letter死信队列
     * @return
     */
    @Bean
    public Queue orderUnpayDeadLetterQueue() {
        return new Queue(this.orderUnpayDeadLetterQueue);
    }

    /**
     * 将死信队列绑定至死信交换机
     * @param orderUnpayDeadLetterQueue
     * @param orderUnpayDeadLetterExchange
     * @return
     */
    @Bean
    public Binding bindDeadLetter(Queue orderUnpayDeadLetterQueue, TopicExchange orderUnpayDeadLetterExchange) {
        return BindingBuilder.bind(orderUnpayDeadLetterQueue)
                .to(orderUnpayDeadLetterExchange)
                .with(orderUnpayDeadLetterRoutingKey);
    }


    /**
     * 声明订单交换机
     * @return
     */
    @Bean
    public TopicExchange orderExchange() {
        return new TopicExchange(this.orderExchange);
    }


    /**
     * 声明 待支付订单队列
     * @return
     */
    @Bean
    public Queue orderUnpayQueue() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", this.orderUnpayDeadLetterExchange);
//       x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", this.orderUnpayDeadLetterRoutingKey);
        //写法1
        return new Queue(this.orderUnpayQueue, true, false, false, args);
        //写法2
        //return QueueBuilder.durable(BUSINESS_QUEUEA_NAME).withArguments(args).build();
    }

    /**
     * 绑定待支付订单队列和交换机
     * @param orderUnpayQueue
     * @param orderExchange
     * @return
     */
    @Bean
    public Binding bindOrderUnpayQueue(Queue orderUnpayQueue, TopicExchange orderExchange) {
        return BindingBuilder.bind(orderUnpayQueue)
                .to(orderExchange)
                .with(this.orderUnpayRoutingKey);
    }


    /**
     * 对rabbitmq的Object类型的消息进行json处理（Object的bean要实现Serializable接口）
     * @return
     */
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
